package com.csii.itest;

import org.apache.poi.hwpf.HWPFDocument;  
import org.apache.poi.hwpf.converter.PicturesManager;  
import org.apache.poi.hwpf.converter.WordToHtmlConverter;  
import org.apache.poi.hwpf.usermodel.Picture;  
import org.apache.poi.hwpf.usermodel.PictureType;  
import org.jsoup.Jsoup;   
import org.w3c.dom.Document;  
  






import javax.xml.parsers.DocumentBuilderFactory;  
import javax.xml.parsers.ParserConfigurationException;  
import javax.xml.transform.OutputKeys;  
import javax.xml.transform.Transformer;  
import javax.xml.transform.TransformerException;  
import javax.xml.transform.TransformerFactory;  
import javax.xml.transform.dom.DOMSource;  
import javax.xml.transform.stream.StreamResult;  

import java.io.*;  
import java.util.List;  

public class testdocx {
	private static FormatConversionpPDF a;
	 public static void main(String argv[]) {  
	        try {  
//	            convert2Html("f:\\1481876412744financeProdFiles.doc","f:\\1481876412744financeProdFiles.html");  
	            FormatConversionpPDF.convertHtmlToPdf("f:\\1481876412744financeProdFiles.html", "f:\\1481876412744financeProdFiles.pdf");
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
//	        try {
//	        	FormatConversionpPDF.convertHtmlToPdf("f:\\1481876412744financeProdFiles.html", "f:\\1481876412744financeProdFiles.pdf");
//			} catch (Exception e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
	    }
	
	// word转html
	 public static void convert2Html(String fileName, String outPutFile)  
	            throws TransformerException, IOException,  
	            ParserConfigurationException {  
	  
	        HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(fileName));  

	        WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(  
	                DocumentBuilderFactory.newInstance().newDocumentBuilder()  
	                        .newDocument());  
	        wordToHtmlConverter.setPicturesManager( new PicturesManager()  
	        {  
	            public String savePicture( byte[] content,  
	                                       PictureType pictureType, String suggestedName,  
	                                       float widthInches, float heightInches )  
	            {  
	                return "test/"+suggestedName;  
	            }  
	        } );  
	        wordToHtmlConverter.processDocument(wordDocument);  
	        //save pictures  
	        List pics=wordDocument.getPicturesTable().getAllPictures();  
	        if(pics!=null){  
	            for(int i=0;i<pics.size();i++){  
	                Picture pic = (Picture)pics.get(i);  
	                try {  
	                	// 判断文件夹是否存在
	                	File imgPath = new File("f:/test");
	                    if(!imgPath.exists()){//图片目录不存在则创建
	                        imgPath.mkdirs();
	                    }
	                    pic.writeImageContent(new FileOutputStream("f:/test/"  
	                            + pic.suggestFullFileName()));  
	                } catch (FileNotFoundException e) {  
	                    e.printStackTrace();  
	                }  
	            }  
	        }  
	        Document htmlDocument = wordToHtmlConverter.getDocument();  
	  
	        ByteArrayOutputStream out = new ByteArrayOutputStream();  
	        DOMSource domSource = new DOMSource(htmlDocument);  
	        StreamResult streamResult = new StreamResult(out);  
	  
	  
	        TransformerFactory tf = TransformerFactory.newInstance();  
	        Transformer serializer = tf.newTransformer();  
	        serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");  
	        serializer.setOutputProperty(OutputKeys.INDENT, "yes");  
	        serializer.setOutputProperty(OutputKeys.METHOD, "HTML");  
	        serializer.transform(domSource, streamResult);  
	        out.close();  
	        
	        writeFile(new String(out.toByteArray()), outPutFile);  
	    }
	 
	 // 输出html文件
	 public static void writeFile(String content, String path) {  
	        FileOutputStream fos = null;   BufferedWriter bw = null;  
	        org.jsoup.nodes.Document doc = Jsoup.parse(content);  
	         content=doc.html();  
	        try {  
	            File file = new File(path);  
	            fos = new FileOutputStream(file);  
	            bw = new BufferedWriter(new OutputStreamWriter(fos,"utf-8"));  
	            bw.write(content);  
	        } catch (FileNotFoundException fnfe) {  
	            fnfe.printStackTrace();  
	        } catch (IOException ioe) {  
	            ioe.printStackTrace();  
	        } finally {  
	            try {  
	                if (bw != null)  
	                    bw.close();  
	                if (fos != null)  
	                    fos.close();  
	            } catch (IOException ie) {  
	            }  
	        }  
	    }

	 
	 

}
